// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 初始化饼状图
    initCharts();
    
    // 初始化评价页面的标签切换功能
    initReviewTabs();
    
    // 初始化点赞功能
    initLikeButtons();
});

// 初始化饼状图
function initCharts() {
    // 检查是否存在visitsChart元素
    const visitsChartElement = document.getElementById('visitsChart');
    if (visitsChartElement) {
        // 游客季节偏好分布饼图
        new Chart(visitsChartElement, {
            type: 'pie',
            data: {
                labels: ['春季', '夏季', '秋季', '冬季'],
                datasets: [{
                    data: [30, 35, 25, 10],
                    backgroundColor: [
                        'rgba(75, 192, 192, 0.7)',
                        'rgba(255, 159, 64, 0.7)',
                        'rgba(255, 205, 86, 0.7)',
                        'rgba(153, 102, 255, 0.7)'
                    ],
                    borderColor: [
                        'rgba(75, 192, 192, 1)',
                        'rgba(255, 159, 64, 1)',
                        'rgba(255, 205, 86, 1)',
                        'rgba(153, 102, 255, 1)'
                    ],
                    borderWidth: 2
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'right',
                        labels: {
                            font: {
                                size: 12
                            },
                            padding: 10
                        }
                    },
                    tooltip: {
                        enabled: false
                    },
                    title: {
                        display: false
                    }
                },
                animation: false
            }
        });
    }
    
    // 检查是否存在satisfactionChart元素
    const satisfactionChartElement = document.getElementById('satisfactionChart');
    if (satisfactionChartElement) {
        // 景点满意度分布饼图
        new Chart(satisfactionChartElement, {
            type: 'pie',
            data: {
                labels: ['非常满意', '满意', '一般', '不满意'],
                datasets: [{
                    data: [45, 35, 15, 5],
                    backgroundColor: [
                        'rgba(54, 162, 235, 0.7)',
                        'rgba(75, 192, 192, 0.7)',
                        'rgba(255, 206, 86, 0.7)',
                        'rgba(255, 99, 132, 0.7)'
                    ],
                    borderColor: [
                        'rgba(54, 162, 235, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(255, 99, 132, 1)'
                    ],
                    borderWidth: 2
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'right',
                        labels: {
                            font: {
                                size: 12
                            },
                            padding: 10
                        }
                    },
                    tooltip: {
                        enabled: false
                    },
                    title: {
                        display: false
                    }
                },
                animation: false
            }
        });
    }
}

// 初始化评价页面的标签切换功能
function initReviewTabs() {
    const tabButtons = document.querySelectorAll('.tab-btn');
    const reviewCards = document.querySelectorAll('.review-card');
    
    if (tabButtons.length > 0) {
        tabButtons.forEach(button => {
            button.addEventListener('click', function() {
                // 移除所有标签的active类
                tabButtons.forEach(btn => btn.classList.remove('active'));
                
                // 给当前点击的标签添加active类
                this.classList.add('active');
                
                const tab = this.getAttribute('data-tab');
                
                // 根据标签显示或隐藏评价卡片
                reviewCards.forEach(card => {
                    if (tab === 'all' || card.classList.contains(tab)) {
                        card.style.display = 'block';
                    } else {
                        card.style.display = 'none';
                    }
                });
            });
        });
    }
}

// 初始化点赞功能
function initLikeButtons() {
    const likeButtons = document.querySelectorAll('.like-btn, .action-btn.upvote');
    
    likeButtons.forEach(button => {
        button.addEventListener('click', function() {
            const likeCount = this.querySelector('.like-count') || this.textContent.match(/\d+/);
            
            if (likeCount) {
                let count = parseInt(likeCount.textContent || likeCount[0]);
                
                if (this.classList.contains('liked')) {
                    // 取消点赞
                    this.classList.remove('liked');
                    count--;
                } else {
                    // 添加点赞
                    this.classList.add('liked');
                    count++;
                }
                
                // 更新点赞数
                if (this.querySelector('.like-count')) {
                    this.querySelector('.like-count').textContent = count;
                } else {
                    this.textContent = this.textContent.replace(/\d+/, count);
                }
            }
        });
    });
}

// 平滑滚动到锚点
function smoothScrollToAnchor(anchorId) {
    const element = document.getElementById(anchorId);
    if (element) {
        element.scrollIntoView({ behavior: 'smooth' });
    }
}

// 表单提交处理
function handleFormSubmit(event) {
    event.preventDefault();
    // 这里可以添加表单验证和提交逻辑
    alert('感谢您的反馈！');
    event.target.reset();
}